//
//  GHBaseDoubleTableVC.swift
//  KYC2C
//
//  Created by Guanghui Liao on 12/18/17.
//  Copyright © 2017 liaoworking. All rights reserved.
//

import UIKit

class GHBaseDoubleTableVC: GHBaseViewController,UITableViewDelegate,UITableViewDataSource,GHStatusViewDelegate{
  
    var leftDataList:Array<Any> = []
    var rightDataList:Array<Any> = []

    lazy var scrollView = {() -> UIScrollView in
        let scrollView = UIScrollView(frame: UIScreen.main.bounds)
        scrollView.contentSize = CGSize(width: K_WIDTH * 2, height: K_HEIGHT - 64)
        scrollView.isPagingEnabled = true
        scrollView.bounces = false
        scrollView.delegate = self
        scrollView.backgroundColor = AppBGColor
        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never;
        }
        return scrollView
    }()
    
    lazy var leftTableView = {() -> UITableView in
        var tableView = UITableView(frame:CGRect(x: 0, y: (self.navigationController?.navigationBar.frame.maxY)!+45, width: K_WIDTH, height: K_HEIGHT - 45 - 64) , style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.red
        tableView.separatorStyle = .none
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never;
        }
        return tableView
    }()
    
    lazy var rightTableView = {() -> UITableView in
        var tableView = UITableView(frame: CGRect(x: K_WIDTH, y: (self.navigationController?.navigationBar.frame.maxY)!+45, width: K_WIDTH, height: K_HEIGHT - 45 - 64), style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.blue
        tableView.separatorStyle = .none
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never;
        }
        return tableView
    }()
    
    lazy var segmentBtn = {() -> GHSegmentBtnView in
        let segmentBtn = GHSegmentBtnView()
        segmentBtn.frame = CGRect(x:0, y:(self.navigationController?.navigationBar.frame.maxY)!, width:K_WIDTH, height:45);
        segmentBtn.delegate = self;
        return segmentBtn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //子类中去设置
        segmentBtn.leftTitle = "左边标题"
        segmentBtn.rightTitle = "右边标题"
        view.addSubview(scrollView)
        view.addSubview(segmentBtn)
        scrollView.addSubview(leftTableView)
        scrollView.addSubview(rightTableView)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}


extension GHBaseDoubleTableVC {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell();
    }
    
    //MARK: - segmentBarViewDelegate
    func gh_bankStatusLeftLabelClick() {
        if (leftTableView.left == -K_WIDTH) {
            UIView.animate(withDuration: 0.25, animations: {
                
                self.scrollView.contentOffset = CGPoint(x: 0, y: 0)
                
            }, completion: { (isOK) in
                //可以在这里加载数据
            })
        } else {
            UIView.animate(withDuration: 0.25, animations: {
                self.scrollView.contentOffset = CGPoint(x: 0, y: 0)
                
            })
            
        }
    }
    
    func gh_bankStatusRightLabelClick() {
        //右边按钮的点击
        if (rightTableView.left == K_WIDTH) {
            UIView.animate(withDuration: 0.25, animations: {

                self.scrollView.contentOffset = CGPoint(x: K_WIDTH, y: 0)
                
            }, completion: { (isOK) in
                //可以在这里加载数据
            })
            
        } else {
            UIView.animate(withDuration: 0.25, animations: {
                self.scrollView.contentOffset = CGPoint(x: K_WIDTH, y: 0)
            })
        }
    }
    
}

extension GHBaseDoubleTableVC{
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        /// 如果是UITableView，就不滑动
        if (scrollView == leftTableView ||
            scrollView == rightTableView) {
            return;
        }
        self.segmentBtn.changeLeft(scrollView.contentOffset.x)
   
        if (scrollView.contentOffset.x == K_WIDTH) {
            if (self.rightDataList.count == 0) {
                //加载右边的数据
            }
        } else if (scrollView.contentOffset.x == 0.00) {
            if (self.leftDataList.count == 0) {
                //加载左边的数据
            }
        }
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if (scrollView.contentOffset.x == K_WIDTH) {
            self.segmentBtn.isScrollToEnd = true
        } else if (scrollView.contentOffset.x == 0.00) {
            self.segmentBtn.isScrollToEnd = false
        }
    }
    
}
